from django.shortcuts import render
from django.shortcuts import redirect
from utils.cos import delete_bucket


def setting(request, project_id):
    return render(request, 'setting.html')


def setting_delete(request, project_id):
    if request.method == 'GET':
        return render(request, 'setting_delete.html')
    project_name = request.POST.get('project_name')
    if project_name != request.tracer.project.name:
        return render(request, 'setting_delete.html', {'error': '项目名称不一致'})
    if request.tracer.user != request.tracer.project.creator:
        return render(request, 'setting_delete.html', {'error': '只有项目创建者可以删除项目'})
    delete_bucket(request.tracer.project.bucket, request.tracer.project.region)
    request.tracer.project.delete()
    return redirect('project_list')
